package leetcode;

/**
 * @Classname AddTwoNumbers_2
 * @Author happytaohaha
 * @Date 2020/3/8
 * @Description 两数相加
 * 1. 分三种情况  l1 l2 都不为空  和 任意其中一个为空
 */
public class AddTwoNumbers_0002 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int flag = 0;
        ListNode result = l1;
        while (l1 != null && l2 != null) {
            int value = l1.val + l2.val + flag;
            flag = value / 10;
            l1.val = value % 10;
            if (l1.next != null) {
                l1 = l1.next;
                l2 = l2.next;
            } else {
                //l1 为空 拼到l1 上
                l1.next = l2.next;
                l2 = null;
                l1 = l1.next;
            }
        }
        while (l1 != null) {
            int value = l1.val + flag;
            flag = value / 10;
            l1.val = value % 10;
            l1 = l1.next;
        }
        l1 = result;
        if (flag == 1) {
            while (l1.next != null) {
                l1 = l1.next;
            }
            l1.next = new ListNode(1);
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
